package com.study.gateway.client.support;

import com.study.common.config.ServiceDefinition;
import com.study.common.config.ServiceInstance;
import com.study.gateway.register.center.api.RegisterCenter;
import com.study.gateway.client.core.ApiProperties;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.ServiceLoader;
@Slf4j
public abstract class AbstractClientRegisterManager {

    @Getter
    private ApiProperties apiProperties;

    private RegisterCenter registerCenter;

    protected AbstractClientRegisterManager(ApiProperties apiProperties){
        this.apiProperties = apiProperties;

        //初始化注册中心对象
        ServiceLoader<RegisterCenter> serviceLoader = ServiceLoader.load(RegisterCenter.class);


        for (RegisterCenter center: serviceLoader) {
            if (center == null){
                log.error("not found RegisterCenter impl");
                break;

            }
            registerCenter = center;
            break;
        }
        if (registerCenter == null){
            throw new RuntimeException("not found RegisterCenter impl");
        }
    }

    protected void register(ServiceDefinition serviceDefinition, ServiceInstance serviceInstance) {
        registerCenter.register(serviceDefinition, serviceInstance);
    }
}